from typing import Dict, Any
from myProfile.models.data.myProfile_models import Login


class LoginRepository:

    def __init__(self, engine):
        self.engine = engine

    async def insert_login(self, details: Dict[str, Any]) -> bool:
        try:
            login = Login(**details)
            await self.engine.save(login)
        except Exception as e:
            print(e)
            return False
        return True

    async def update_login(self, id: int, details: Dict[str, Any]) -> bool:
        try:
            login = await self.engine.find_one(Login, Login.id == id)
            for key, value in details.items():
                setattr(login, key, value)
            await self.engine.save(login)
        except Exception as e:
            print(e)
            return False
        return True

    async def delete_login(self, id: int) -> bool:
        try:
            login = await self.engine.find_one(Login, Login.id == id)
            await self.engine.delete(login)
        except Exception:
            return False
        return True

    async def get_login(self, id: int) -> Login:
        login = await self.engine.find_one(Login, Login.id == id)
        return login

    async def get_all_login(self) -> list:
        logins = await self.engine.find(Login)
        return logins

    async def get_login_username(self, username: str) -> Login:
        login = await self.engine.find_one(Login, Login.username == username)
        return login
